<template>
  <t-dialog :header="header" :visible.sync="visible" :onClose="closeDialog" :closeOnOverlayClick="false"
    @confirm="onSubmit" :confirmBtn="{
      content: loading ? '提交中...' : '确认',
      theme: 'primary',
      loading: loading,
    }" width="50%" top="5%">
    <t-form ref="form" :colon="true" :data="formData" labelWidth="100px" :rules="rules">
      <t-form-item label="上级指标" name="parentId">
        <t-tree-select clearable filterable :treeProps="{ keys: { value: 'id' } }" v-model="formData.parentId"
          :data="menuOptions" placeholder="没有上级指标" />
      </t-form-item>
      <t-form-item label="显示排序" name="sort">
        <t-input-number clearable v-model="formData.sort" :min="1" placeholder="请输入" />
      </t-form-item>
      <t-form-item label="指标名称" name="name">
        <t-input clearable v-model="formData.name" placeholder="请输入指标名称" />
      </t-form-item>
      <t-form-item label="指标类别" name="type">
        <t-select style="width: 240px" clearable v-model="formData.type" :options="dict.type.kjpt_system_type">
        </t-select>
      </t-form-item>
      <t-form-item label="上限分" name="limitScore" v-if="this.formData.type == '1'">
        <t-input-number clearable v-model="formData.limitScore" :min="1" placeholder="请输入" />
      </t-form-item>
      <t-form-item label="考核内容分" name="score" v-if="this.formData.type == '2'">
        <t-input-number clearable v-model="formData.score" :min="1" placeholder="请输入" />
      </t-form-item>
      <t-form-item label="备注" name="note">
        <t-input clearable v-model="formData.note" placeholder="请输入" />
      </t-form-item>
    </t-form>
  </t-dialog>
</template>

<script>
export default {
  dicts: ['kjpt_system_type'],
  props: ['header', 'majorId', 'parentId'],
  data () {
    return {
      rules: {
        // parentId: [
        //   { required: true, message: "上级专业不能为空" }
        // ],
        name: [
          { required: true, message: "指标名称不能为空" }
        ],
        type: [
          { required: true, message: "指标类别不能为空" }
        ],
        sort: [
          { required: true, message: "显示排序不能为空" }
        ],
        limitScore: [
          {
            validator: (val) => (this.formData.type == '1' && val > 0) || this.formData.type != '1', message: "上限分不能为空", type: "error"
          },
        ],
        score: [
          {
            validator: (val) => (this.formData.type == '2' && val > 0) || this.formData.type != '2', message: "考核内容分不能为空", type: "error"
          },
        ],
      },
      menuOptions: [],
      visible: true,
      loading: false,
      formData: {
        parentId: null,
        name: "",
        type: '',
        sort: 1,
        limitScore: null,
        score: null,
        note: null,
      }
    }
  },
  watch: {
    majorId: {
      immediate: true,
      handler (val) {
        if (val) {
          this.getObj();
        }
      }
    },
    parentId: {
      immediate: true,
      handler (val) {
        if (val) {
          this.formData.parentId = val;
        }
      }
    }
  },
  created () {
    if (!this.majorId) {
      this.getTreeselect();
    }
  },
  methods: {
    getObj () {
      this.$api.kjpt.point.tree().then(response => {
        this.menuOptions = [];
        this.menuOptions = response.data;
        this.$api.kjpt.point.get(this.majorId).then(response => {
          if (response.data.parentId === 0) {
            response.data.parentId = null;
          }
          this.formData = response.data;
        }).catch((e) => {
          this.$message.error(e.toString());
        });
      }).catch((e) => {
        this.$message.error(e.toString());
      });

    },
    onSubmit () {

      this.$refs.form.validate().then((validateResult) => {

        if (validateResult === true) {

          this.loading = true;
          if (this.majorId) {
            let { parentId } = this.formData;
            if (this.majorId === this.formData.parentId || !this.formData.parentId) {
              parentId = 0;
            }
            const row = { ...this.formData, parentId }
            this.$api.kjpt.point.edit(row).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            let { parentId } = this.formData;
            if (!this.formData.parentId) {
              parentId = 0;
            }
            const row = { ...this.formData, parentId }
            this.$api.kjpt.point.add(row).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
    closeDialog () {
      this.$emit("update:visible", false);
    },
    /** 查询菜单下拉树结构 */
    getTreeselect () {
      this.$api.kjpt.point.tree().then(response => {
        this.menuOptions = [];
        this.menuOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
  }
}
</script>

<style scoped>
.overlay-options {
  display: inline-block;
  font-size: 20px;
}
</style>
